#include "report_handle.h"
static TaskHandle_t thread_report;
iot_dazzling_info_t status_dazzling_info; 
static u8 buffer_send[SIZE];
void iot_report_init()
{
    memset(&status_dazzling_info,0,sizeof(iot_dazzling_info_t));
}

extern char buf_device[17];
void report_dazzling_handle()
{
    if (0 != send_num)
    {
        int i=0;
        buffer_send[0]= 0xA5;
        i +=1;
        //DI
        memcpy(&buffer_send[1+2],buf_device,16);
        send_num+=16;
        i+=16;
        //MI
        u32 mess;
        mess = get_message();
        memcpy(&buffer_send[1+16+2],&mess,4);
        send_num +=4;
        i +=4;
        memcpy(&buffer_send[1],&send_num,sizeof(u16));
        i+= sizeof(u16);
        memcpy(&buffer_send[i],buffer,(send_num-4-16));
        i+=(send_num-4-16);
        buffer_send[i] =0;
        i +=1;
        buffer_send[i] =0;
        i +=1;
        buffer_send[i] =0x5A;
        i +=1;
        TCP_send_data(buffer_send,i); //TCP发送
        uart1_msg_send(buffer_send,i); //串口发送
        memset(buffer,0,send_num);
        memset(buffer_send,0,i);
        i=0;
        send_num = 0;
    }
}

void iot_report_handle()
{
    static int time;



    /*********************主模式相关设置******************************************************/
    if (status_dazzling_info.main_ssp != app_logic_get_ssp())
    {
        status_dazzling_info.main_ssp= app_logic_get_ssp();
        change_main_mode_handle(MAIN_MODE,status_dazzling_info.main_ssp);
    }

    /*********************方向设置*************************************************************/
    if(status_dazzling_info.dire != get_light_direction())
    {
        status_dazzling_info.dire = get_light_direction();
        change_light_direction_handle(DIRECTION,status_dazzling_info.dire);
    }
    /*********************RGB灯珠相关设置******************************************************/
    if (status_dazzling_info.brighness_level != get_brighness_level())
    {
        status_dazzling_info.brighness_level = get_brighness_level();
        change_RGB_brighness_handle(RGB_BRIGHNESS,status_dazzling_info.brighness_level);
    }

    /*********************场景相关相关设置******************************************************/
    if(status_dazzling_info.scene_mode != get_scene_ssp())
    {
        status_dazzling_info.scene_mode = get_scene_ssp();
        change_scene_mode_handle(SCENE_MODE,status_dazzling_info.scene_mode);
    }//场景模式子模式切换

    if (status_dazzling_info.scene_senet != get_scene_senet())
    {
       status_dazzling_info.scene_senet = get_scene_senet();
       change_scene_senet_handle(SCENE_SENST,status_dazzling_info.scene_senet);
    }//场景模式子模式灵敏度切换

    /*********************自定义相关相关设置******************************************************/
    if(status_dazzling_info.custom_ssp != get_custom_ssp())
    {
        status_dazzling_info.custom_ssp = get_custom_ssp();
        change_custom_ssp_handle(CUSTOM_SSP,status_dazzling_info.custom_ssp);
    } //自定义模式子模式

    if((status_dazzling_info.custom_part.len != light_effect_config.len)||memcmp(status_dazzling_info.custom_part.RGB,light_effect_config.buf,light_effect_config.len))
    {
        status_dazzling_info.custom_part.len = light_effect_config.len;
        memcpy(status_dazzling_info.custom_part.RGB,light_effect_config.buf,light_effect_config.len);
        change_custom_part_handle(CUSTOM_PART_DATA,status_dazzling_info.custom_part);
    } //自定义模式 预览分段长度及内容

    if(status_dazzling_info.wait != get_light_wait())
    {
        status_dazzling_info.wait = get_light_wait();
        change_custom_wait_handle(CUSTOM_WAIT,status_dazzling_info.wait);
    } //自定义模式 预览 等待时间

    if(status_dazzling_info.light_effect_ssp != get_light_effect_enum())
    {
        status_dazzling_info.light_effect_ssp = get_light_effect_enum();
        change_light_effect_ssp_handle(EFFECT_SSP,status_dazzling_info.light_effect_ssp);
    } //自定义模式 效果模式


    /*********************麦克风相关设置******************************************************/
    if(status_dazzling_info.micro_senst != get_senst_level()) //灵敏度
    {
        status_dazzling_info.micro_senst = get_senst_level();
        change_micro_senst_handle(MICRO_SENST,status_dazzling_info.micro_senst);
    }

    if(status_dazzling_info.spectral_mode != get_micro_ssp())  //子模式
    {
        status_dazzling_info.spectral_mode = get_micro_ssp();
        change_spectral_mode_handle(SPECTRAL_MODE,status_dazzling_info.spectral_mode);
    }

    /*********************分段模式相关的相关相关设置******************************************************/
    if((status_dazzling_info.part_info.len != part_info.len) || memcmp(status_dazzling_info.part_info.RGB,part_info.RGB,part_info.len*3))
    {
        status_dazzling_info.part_info.len = part_info.len;
        memcpy(status_dazzling_info.part_info.RGB,part_info.RGB,part_info.len*3);
        change_part_info_handle(PART_DATA,status_dazzling_info.part_info);
    }

    report_dazzling_handle(); //上报处理函数

    if(arch_os_ms_elapsed(time)>=(30*1000))//每30s上报一次心跳
    {
        heart_beat_handle();
        time=arch_os_ms_now();
    }
}

void iot_report_loop(void *pvParameters)
{
    LOG_INFO("INTO iot_report_loop\n"); 
    while (1)
    {
        iot_report_handle();
        vTaskDelay(pdMS_TO_TICKS(IOT_REPORTE_SPEED_MS)); //延时20ms
    }
}


void task_iot_report_init()
{
    xTaskCreate(iot_report_loop,"iot_report_dazzling_loop",REPORT_LOGIC_SZIE,NULL,TASK_PROP_IOTTALK,&thread_report);
}